package com.iot.device.utils;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.iot.common.utils.http.HttpUtils;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;

public class BaiduLocationUtils {
	public static String getLocation(double longitude, double latitude) {
		String location = "";
		try {
			//百度地图获取地址			
			JSONObject address = baiduGeocoder(longitude, latitude).getJSONObject("result").getJSONObject("addressComponent");
			if(address.getString("province").equals(address.getString("city"))) {
				location = address.getString("province")+address.getString("district");
			}else {
				location = address.getString("province")+address.getString("city")+address.getString("district");
			}
			
			if(!location.equals("")) {
				return location;
			}
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		try {
			JSONObject obj = JSONObject.parseObject(HttpUtils.sendGet("https://restapi.amap.com/v3/geocode/regeo", "key=a809117c4fa6694baf1f5c584f5df983&location="+longitude+","+latitude));
			JSONObject address = obj.getJSONObject("regeocode").getJSONObject("addressComponent");
			location = address.getString("province")+address.getString("city")+address.getString("district");
			location = location.replaceAll("\\[", "").replaceAll("\\]", "");
			if(location.equals("中华人民共和国")) {
				location = address.getString("seaArea");
				if(!location.equals("")) {
					location = "中国"+location+"海域";
				}
			}
			if(!location.equals("")) {
				return location;
			}
		}catch (Exception e) {
			e.printStackTrace();
		}
		
		return "("+longitude+", "+latitude+")";
	}
	
	public static JSONObject baiduGeocoder(double longitude, double latitude) {
			//百度地图获取地址			
		String json = HttpUtils.sendGet("http://api.map.baidu.com/geocoder/v2/", "callback=renderReverse&location="+latitude+","+longitude+"&output=json&pois=1&ak=Vaz2b04EoRifFPki9yCA3P0L1hDHWLl6");
		return JSONObject.parseObject(json.substring(json.indexOf("&&renderReverse")+16, json.length()-1));
	}

	public static String getAddress(double longitude, double latitude) {
			//百度地图获取地址
		String json = HttpUtils.sendGet("http://api.map.baidu.com/geocoder/v2/", "callback=renderReverse&location="+latitude+","+longitude+"&output=json&pois=1&ak=Vaz2b04EoRifFPki9yCA3P0L1hDHWLl6");
		JSONObject result = JSONObject.parseObject(json).getJSONObject("result");
		return result.getString("formatted_address_poi");
	}
	
//	周边获取受影响市县乡镇
	public static JSONObject getRegion(float longitude, float latitude, int radius) {
		URL url;
		JSONObject json = new JSONObject();
		try {
			url = new URL("http://api.map.baidu.com/place/v2/search?query=%E4%B9%A1%E9%95%87&location="+latitude+","+longitude+"&radius="+radius+"&output=json&coord_type=2&page_size=20&ak=Vaz2b04EoRifFPki9yCA3P0L1hDHWLl6");
			HttpURLConnection connection = (HttpURLConnection) url.openConnection();
			InputStream is = connection.getInputStream();
			InputStreamReader inputStreamReader = new InputStreamReader(is,"UTF-8");
            BufferedReader reader = new BufferedReader(inputStreamReader);
            
            String tempLine;
            StringBuffer resultBuffer = new StringBuffer();
			while ((tempLine = reader.readLine()) != null) {
                
				resultBuffer.append(tempLine);
            }
			String location = resultBuffer.toString();
			JSONArray objs = JSONObject.parseObject(location).getJSONArray("results");
			
			
			String address = "";
			for (int i = 0; i < objs.size(); i++) {
				address = objs.getJSONObject(i).getString("city")+objs.getJSONObject(i).getString("area");
				if(json.getJSONArray(address)==null) {
					json.put(address, new JSONArray());
				}
				json.getJSONArray(address).add(objs.getJSONObject(i).getString("name"));
			}
			return json;
		} catch (MalformedURLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return json;
	}
	
//	周边获取受影响市县乡镇
	public static JSONArray getArea(String query, float longitude, float latitude, int radius) {
		try {
			URL url = new URL("http://api.map.baidu.com/place/v2/search?query="+query+"&location="+latitude+","+longitude+"&radius="+radius+"&output=json&coord_type=2&page_size=20&ak=Vaz2b04EoRifFPki9yCA3P0L1hDHWLl6");
			HttpURLConnection connection = (HttpURLConnection) url.openConnection();
			InputStream is = connection.getInputStream();
			InputStreamReader inputStreamReader = new InputStreamReader(is,"UTF-8");
            BufferedReader reader = new BufferedReader(inputStreamReader);
            
            String tempLine;
            StringBuffer resultBuffer = new StringBuffer();
			while ((tempLine = reader.readLine()) != null) {
                
				resultBuffer.append(tempLine);
            }
			String location = resultBuffer.toString();
			JSONArray results = JSONObject.parseObject(location).getJSONArray("results");
			if(results==null) {
				results = new JSONArray();
			}
			return results;
		} catch (MalformedURLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return new JSONArray();
	}
	
	public static void main(String[] args) {
		System.out.println(getLocation(104.6374,38.743458));
		System.out.println(getLocation(116.310003,39.991957));
		System.out.println(getLocation(102.74,37.88));
		System.out.println(getLocation(120.938053,38.520982));
		System.out.println(getLocation(124.561741,39.0172));
	}
}
